package org.mics.organization.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.mics.core.page.PageInfo;
import org.mics.core.page.PageQuery;
import org.mics.core.response.PageDataResponse;
import org.mics.lang.bean.BeanConvertUtil;
import org.mics.organization.entity.PostDO;
import org.mics.organization.repository.PostRepository;
import org.mics.organization.request.PostRequest;
import org.mics.organization.service.PostService;
import org.mics.organization.vo.PostVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * 岗位
 * @author mics
 * @date 2023-10-9
 * @version  1.0
 */
@Service
public class PostServiceImpl  implements PostService{
	@Resource
	private PostRepository postRepository;

	@Override
	@Transactional
	public String add(PostRequest postRequest) {
		PostDO postDO  = BeanConvertUtil.convert(postRequest, PostDO.class);
		postRepository.save(postDO);
		return postDO.getId();
	}

	@Override
	@Transactional
	public void delete(String id) {
		postRepository.deleteById(id);
	}

	@Override
	public PageDataResponse<PostVO> listByPage(PageQuery pageQuery) {
		//规格定义
        Specification<PostDO> specification = new Specification<PostDO>() {
			private static final long serialVersionUID = -8785309034575411806L;
			/**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<PostDO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            	List<Predicate> lstPredicates = new ArrayList<  >();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				if(StringUtils.isNotBlank(pageQuery.getKey())) {
					lstPredicates.add(cb.like(root.get("name").as(String.class),"%"+pageQuery.getKey()+"%"));
				}
				return cb.and(lstPredicates.toArray(arrayPredicates));
            }
        };
        Pageable pageable = PageRequest.of(pageQuery.getPageNo(),pageQuery.getPageSize(), Sort.Direction.DESC, "createDate");
        //查询
       Page<PostDO> page = this.postRepository.findAll(specification,pageable);
       PageInfo pageInfo = new PageInfo();
       pageInfo.setPageNo(page.getNumber());
       pageInfo.setPageSize(page.getSize());
       pageInfo.setTotalCount(page.getTotalElements());
       pageInfo.setTotalPage(page.getTotalPages());
       List<PostVO> pageList = BeanConvertUtil.convertList(page.getContent(), PostVO.class);
      return new PageDataResponse<>(pageInfo,pageList);
	}

	@Override
	public List<PostDO> list() {
		return postRepository.findAll();
	}
}
